<code class="prism language-python has-numbering" onclick="mdcp.signin(event)" style="position: unset;"><span
        class="token comment"># -*- coding: utf-8 -*-</span>
    <span class="token keyword">import</span> psutil
    <span class="token keyword">import</span> requests
    <span class="token keyword">import</span> json
    <span class="token keyword">import</span> time
    <span class="token keyword">import</span> socket

    <span class="token comment"># 获取本机磁盘使用率和剩余空间G信息</span>
    <span class="token keyword">def</span> <span class="token function">get_disk_info</span><span
            class="token punctuation">(</span><span class="token punctuation">)</span><span
            class="token punctuation">:</span>
    <span class="token triple-quoted-string string">"""
    获取磁盘信息
    :return:  磁盘总容量，磁盘已使用（单位G）
    """</span>
    total_size_all <span class="token operator">=</span> <span class="token number">0</span>
    free_disk_size_all <span class="token operator">=</span> <span class="token number">0</span>
    <span class="token comment"># 循环磁盘分区</span>
    <span class="token keyword">for</span> disk <span class="token keyword">in</span> psutil<span
            class="token punctuation">.</span>disk_partitions<span class="token punctuation">(</span><span
            class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token comment"># 读写方式 光盘 or 有效磁盘类型</span>
    <span class="token keyword">if</span> <span class="token string">'cdrom'</span> <span
            class="token keyword">in</span> disk<span class="token punctuation">.</span>opts <span
            class="token keyword">or</span> disk<span class="token punctuation">.</span>fstype <span
            class="token operator">==</span> <span class="token string">''</span><span
            class="token punctuation">:</span>
    <span class="token keyword">continue</span>
    disk_name_arr <span class="token operator">=</span> disk<span class="token punctuation">.</span>device<span
            class="token punctuation">.</span>split<span class="token punctuation">(</span><span class="token string">':'</span><span
            class="token punctuation">)</span>
    disk_name <span class="token operator">=</span> disk_name_arr<span class="token punctuation">[</span><span
            class="token number">0</span><span class="token punctuation">]</span>
    mountpoint <span class="token operator">=</span> disk<span class="token punctuation">.</span>mountpoint
    disk_info <span class="token operator">=</span> psutil<span class="token punctuation">.</span>disk_usage<span
            class="token punctuation">(</span>mountpoint<span class="token punctuation">)</span>
    <span class="token comment"># 磁盘总的空间，单位G</span>
    <span class="token comment">#total_size = disk_info.total / (1024 * 1024 * 1024)</span>
    total_size <span class="token operator">=</span> disk_info<span class="token punctuation">.</span>total
    <span class="token comment"># 磁盘剩余空间，单位G</span>
    <span class="token comment">#free_disk_size = disk_info.free / (1024 * 1024 * 1024)</span>
    free_disk_size <span class="token operator">=</span> disk_info<span class="token punctuation">.</span>free
    total_size_all <span class="token operator">+=</span> total_size
    free_disk_size_all <span class="token operator">+=</span> free_disk_size
    use_disk_size <span class="token operator">=</span> total_size_all <span class="token operator">-</span>
    free_disk_size_all
    use_info <span class="token operator">=</span> <span class="token string">"%.2f%%"</span> <span
            class="token operator">%</span> <span class="token punctuation">(</span>use_disk_size <span
            class="token operator">/</span> total_size_all<span class="token punctuation">)</span>
    <span class="token keyword">return</span> total_size_all<span class="token punctuation">,</span> use_disk_size<span
            class="token punctuation">,</span> use_info


    <span class="token comment"># cpu信息</span>
    <span class="token keyword">def</span> <span class="token function">get_cpu_info</span><span
            class="token punctuation">(</span><span class="token punctuation">)</span><span
            class="token punctuation">:</span>
    <span class="token triple-quoted-string string">"""
    获取CPU使用率
    :return:  使用率，（%无小数）
    """</span>
    cpu_percent <span class="token operator">=</span> psutil<span class="token punctuation">.</span>cpu_percent<span
            class="token punctuation">(</span>interval<span class="token operator">=</span><span
            class="token number">1</span><span class="token punctuation">)</span>
    cpu_info <span class="token operator">=</span> <span class="token string">"%.2f%%"</span> <span
            class="token operator">%</span> cpu_percent
    <span class="token keyword">return</span> cpu_info


    <span class="token comment"># 内存信息</span>
    <span class="token keyword">def</span> <span class="token function">get_memory_info</span><span
            class="token punctuation">(</span><span class="token punctuation">)</span><span
            class="token punctuation">:</span>
    <span class="token triple-quoted-string string">"""
    获取设备内存信息
    :return:  内存总容量，内存使用量, 内存空余量（单位G），内存使用率（%无小数）
    """</span>
    virtual_memory <span class="token operator">=</span> psutil<span
            class="token punctuation">.</span>virtual_memory<span class="token punctuation">(</span><span
            class="token punctuation">)</span>
    used_memory <span class="token operator">=</span> virtual_memory<span class="token punctuation">.</span>used <span
            class="token operator">/</span> <span class="token punctuation">(</span><span
            class="token number">1024</span><span class="token operator">*</span><span
            class="token number">1024</span><span class="token operator">*</span><span
            class="token number">1024</span><span class="token punctuation">)</span>
    <span class="token comment">#free_memory = virtual_memory.free / 1024 / 1024 / 1024</span>
    total_memory <span class="token operator">=</span> virtual_memory<span class="token punctuation">.</span>total <span
            class="token operator">/</span> <span class="token punctuation">(</span><span
            class="token number">1024</span><span class="token operator">*</span><span
            class="token number">1024</span><span class="token operator">*</span><span
            class="token number">1024</span><span class="token punctuation">)</span>
    free_memory <span class="token operator">=</span> total_memory <span class="token operator">-</span> used_memory
    memory_percent <span class="token operator">=</span> <span class="token string">"%.2f%%"</span> <span
            class="token operator">%</span> virtual_memory<span class="token punctuation">.</span>percent
    <span class="token keyword">return</span> total_memory<span class="token punctuation">,</span> used_memory<span
            class="token punctuation">,</span> free_memory<span class="token punctuation">,</span> memory_percent


    <span class="token keyword">def</span> <span class="token function">get_host_ip</span><span
            class="token punctuation">(</span><span class="token punctuation">)</span><span
            class="token punctuation">:</span>
    <span class="token triple-quoted-string string">"""
    查询本机ip地址
    :return: ip
    """</span>
    <span class="token keyword">try</span><span class="token punctuation">:</span>
    s <span class="token operator">=</span> socket<span class="token punctuation">.</span>socket<span
            class="token punctuation">(</span>socket<span class="token punctuation">.</span>AF_INET<span
            class="token punctuation">,</span> socket<span class="token punctuation">.</span>SOCK_DGRAM<span
            class="token punctuation">)</span>
    s<span class="token punctuation">.</span>connect<span class="token punctuation">(</span><span
            class="token punctuation">(</span><span class="token string">'8.8.8.8'</span><span
            class="token punctuation">,</span> <span class="token number">80</span><span
            class="token punctuation">)</span><span class="token punctuation">)</span>
    ip <span class="token operator">=</span> s<span class="token punctuation">.</span>getsockname<span
            class="token punctuation">(</span><span class="token punctuation">)</span><span
            class="token punctuation">[</span><span class="token number">0</span><span
            class="token punctuation">]</span>
    <span class="token keyword">finally</span><span class="token punctuation">:</span>
    s<span class="token punctuation">.</span>close<span class="token punctuation">(</span><span
            class="token punctuation">)</span>

    <span class="token keyword">return</span> ip


    <span class="token keyword">def</span> <span class="token function">restful_api</span><span
            class="token punctuation">(</span>url<span class="token punctuation">,</span> method<span
            class="token punctuation">,</span> args<span class="token operator">=</span><span
            class="token boolean">None</span> <span class="token punctuation">,</span>TK<span
            class="token operator">=</span><span class="token boolean">None</span> <span
            class="token punctuation">,</span>value_json<span class="token operator">=</span><span
            class="token boolean">None</span><span class="token punctuation">)</span><span
            class="token punctuation">:</span>
    <span class="token triple-quoted-string string">''' *函数说明*: 执行Restful API请求

        *参数说明*:
           url:         HTTP请求的地址, 例如: http://api/equip_group/add
           method:      HTTP请求方式, 例如:  GET, POST, PUT, DELETE...
           args:        HTTP请求参数, 以字典形式提供
           TK:          Cookie: TK=F4823B61-2F37-40F9-9212-3BA17F5DEE81-3169E157-5C80-407A-8E45-20A3741DC1FE
           value_json   args仅仅value需要json格式
        *example*:
            restful_api  ${add_group}  ${post}  @{data}  TK=${TK}

    '''</span>
    ret <span class="token operator">=</span> <span class="token string">''</span>
    headers <span class="token operator">=</span> <span class="token punctuation">{<!-- --></span><span
            class="token string">"Content-Type"</span><span class="token punctuation">:</span> <span
            class="token string">"application/json; charset=UTF-8"</span><span class="token punctuation">}</span>

    s <span class="token operator">=</span> requests<span class="token punctuation">.</span>session<span
            class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token keyword">for</span> i <span class="token keyword">in</span> <span
            class="token builtin">range</span><span class="token punctuation">(</span><span
            class="token number">5</span><span class="token punctuation">)</span><span
            class="token punctuation">:</span>
    <span class="token keyword">try</span><span class="token punctuation">:</span>
    <span class="token keyword">if</span> method <span class="token operator">==</span> <span class="token string">'post'</span><span
            class="token punctuation">:</span>
    <span class="token keyword">if</span> value_json<span class="token punctuation">:</span>
    <span class="token keyword">for</span> key <span class="token keyword">in</span> args<span
            class="token punctuation">:</span>
    v <span class="token operator">=</span> args<span class="token punctuation">[</span>key<span
            class="token punctuation">]</span>
    args<span class="token punctuation">[</span>key<span class="token punctuation">]</span> <span
            class="token operator">=</span> json<span class="token punctuation">.</span>dumps<span
            class="token punctuation">(</span>v<span class="token punctuation">)</span>
    ret <span class="token operator">=</span> s<span class="token punctuation">.</span>post<span
            class="token punctuation">(</span>url<span class="token punctuation">,</span> data<span
            class="token operator">=</span>args<span class="token punctuation">,</span> headers <span
            class="token operator">=</span> headers<span class="token punctuation">,</span> timeout<span
            class="token operator">=</span><span class="token number">120</span><span class="token punctuation">)</span>
    <span class="token keyword">else</span><span class="token punctuation">:</span>
    ret <span class="token operator">=</span> s<span class="token punctuation">.</span>post<span
            class="token punctuation">(</span>url<span class="token punctuation">,</span> data<span
            class="token operator">=</span>args<span class="token punctuation">,</span> headers <span
            class="token operator">=</span> headers<span class="token punctuation">,</span> timeout<span
            class="token operator">=</span><span class="token number">120</span><span class="token punctuation">)</span>
    <span class="token keyword">elif</span> method <span class="token operator">==</span> <span class="token string">'get'</span><span
            class="token punctuation">:</span>
    ret <span class="token operator">=</span> s<span class="token punctuation">.</span>get<span
            class="token punctuation">(</span>url<span class="token punctuation">,</span> data<span
            class="token operator">=</span>args<span class="token punctuation">,</span> headers <span
            class="token operator">=</span> headers<span class="token punctuation">,</span> timeout<span
            class="token operator">=</span><span class="token number">120</span><span class="token punctuation">)</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span>ret<span
            class="token punctuation">.</span>content<span class="token punctuation">)</span>
    <span class="token keyword">return</span> ret<span class="token punctuation">.</span>content
    <span class="token keyword">except</span> Exception <span class="token keyword">as</span> e<span
            class="token punctuation">:</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span>e<span
            class="token punctuation">)</span>


    <span class="token keyword">if</span> __name__ <span class="token operator">==</span> <span class="token string">'__main__'</span><span
            class="token punctuation">:</span>
    <span class="token keyword">while</span> <span class="token boolean">True</span><span
            class="token punctuation">:</span>
    ticks <span class="token operator">=</span> time<span class="token punctuation">.</span>time<span
            class="token punctuation">(</span><span class="token punctuation">)</span> <span
            class="token comment"># 时间戳</span>
    total_size_all<span class="token punctuation">,</span> use_disk_size<span class="token punctuation">,</span>
    use_info <span class="token operator">=</span> get_disk_info<span class="token punctuation">(</span><span
            class="token punctuation">)</span> <span class="token comment"># 磁盘信息</span>
    cpu_info <span class="token operator">=</span> get_cpu_info<span class="token punctuation">(</span><span
            class="token punctuation">)</span> <span class="token comment"># cpu使用率</span>
    total_memory<span class="token punctuation">,</span> used_memory<span class="token punctuation">,</span> free_memory<span
            class="token punctuation">,</span> memory_percent <span class="token operator">=</span> get_memory_info<span
            class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment"># 内存信息</span>
    host_ip <span class="token operator">=</span> get_host_ip<span class="token punctuation">(</span><span
            class="token punctuation">)</span>
    url <span class="token operator">=</span> <span class="token string">'发送到的地址'</span>
    args <span class="token operator">=</span> <span class="token punctuation">{<!-- --></span><span
            class="token string">'host_ip'</span> <span class="token punctuation">:</span>host_ip<span
            class="token punctuation">,</span> <span class="token string">'total_size_all'</span><span
            class="token punctuation">:</span> total_size_all<span class="token punctuation">,</span> <span
            class="token string">'use_disk_size'</span> <span class="token punctuation">:</span> use_disk_size<span
            class="token punctuation">,</span> <span class="token string">'use_info'</span><span
            class="token punctuation">:</span>use_info<span class="token punctuation">,</span> <span
            class="token string">'cpu_info'</span><span class="token punctuation">:</span> cpu_info<span
            class="token punctuation">,</span> <span class="token string">'total_memory'</span><span
            class="token punctuation">:</span>total_memory<span class="token punctuation">,</span> <span
            class="token string">'used_memory'</span> <span class="token punctuation">:</span>used_memory<span
            class="token punctuation">,</span> <span class="token string">'free_memory'</span><span
            class="token punctuation">:</span> free_memory<span class="token punctuation">,</span> <span
            class="token string">'memory_percent'</span><span class="token punctuation">:</span> memory_percent<span
            class="token punctuation">,</span> <span class="token string">'time'</span> <span class="token punctuation">:</span>
    ticks<span class="token punctuation">}</span>
    args <span class="token operator">=</span> json<span class="token punctuation">.</span>dumps<span
            class="token punctuation">(</span>args<span class="token punctuation">)</span>
    restful_api<span class="token punctuation">(</span>url<span class="token punctuation">,</span> <span
            class="token string">'post'</span><span class="token punctuation">,</span> args<span
            class="token punctuation">)</span>
    <div class="hljs-button signin" data-title="登录后复制"
         data-report-click="{&quot;spm&quot;:&quot;1001.2101.3001.4334&quot;}"></div>
</code>